import { defineStore } from 'pinia'
import { CACHE_KEY, useCache } from '@/hooks/basic/useCache'
import { getAccessToken, removeToken } from '@/utils/business/auth'
// import { formatDate } from '@/utils/basic-common/formatTime'
import { getInfo, getUserProfile } from '@/api/user/index'
import { store } from '../index'

const { wsCache } = useCache()

interface UserVO {
  id: number
  avatar: string
  nickname: string
}
interface UserProfileVO {
  isLoad: boolean
  id: number | ''
  avatar: string
  nickname: string
  sex: number | ''
  email: string
  mobile: string
  deptName: string
  posts: Array<{ id: number; name: string }>
  roles: Array<{ id: number; name: string }>
  socialUsers: Array<any>
  createTime: string
}
interface UserInfoVO {
  permissions: string[]
  roles: string[]
  isSetUser: boolean
  user: UserVO
  userProfile: UserProfileVO
}

export const useUserStore = defineStore('user', {
  state: (): UserInfoVO => ({
    permissions: [],
    roles: [],
    isSetUser: false,
    user: {
      id: 0,
      avatar: '',
      nickname: ''
    },
    userProfile: {
      isLoad: false,
      id: '',
      avatar: '',
      nickname: '',
      sex: '',
      email: '',
      mobile: '',
      deptName: '',
      posts: [],
      roles: [],
      socialUsers: [],
      createTime: ''
    }
  }),
  getters: {
    getPermissions(): string[] {
      return this.permissions
    },
    getRoles(): string[] {
      return this.roles
    },
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserVO {
      return this.user
    }
  },
  actions: {
    resetState() {
      this.permissions = []
      this.roles = []
      this.isSetUser = false
      this.user = {
        id: 0,
        avatar: '',
        nickname: ''
      }
      this.userProfile = {
        isLoad: false,
        id: '',
        avatar: '',
        nickname: '',
        sex: '',
        email: '',
        mobile: '',
        deptName: '',
        posts: [],
        roles: [],
        socialUsers: [],
        createTime: ''
      }
    },
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (!userInfo) {
        userInfo = await getInfo()
      }
      this.permissions = userInfo.permissions
      this.roles = userInfo.roles
      this.user = userInfo.user
      this.isSetUser = true
      wsCache.set(CACHE_KEY.USER, userInfo)
      wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
    },
    async getUserProfileData() {
      const data = await getUserProfile()
      if (data) {
        this.userProfile.isLoad = true
        this.userProfile.id = data.id
        this.userProfile.nickname = data.nickname
        this.userProfile.avatar = data.avatar
        this.userProfile.sex = data.sex
        this.userProfile.mobile = data.mobile
        this.userProfile.email = data.email
        this.userProfile.deptName = data.dept?.name
        this.userProfile.posts = data.posts || []
        this.userProfile.roles = data.roles || []
        this.userProfile.socialUsers = data.socialUsers || []
        // this.userProfile.createTime = formatDate(data.createTime)
      }
    },
    async loginOut() {
      // await loginOut()
      removeToken()
      wsCache.clear()
      this.resetState()
    },
  }
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
